class News < ActiveRecord::Base
  validates_presence_of :body, :title

  def next
    self.class.select(:id).first(:conditions => ["id > ?", self.id], :limit => 1, :order => "id")
  end

  def previous
    self.class.select(:id).first(:conditions => ["id < ?", self.id], :limit => 1, :order => "id DESC")
  end

  def as_json(options = {})
    next_news = self.next
    prev_news = self.previous
    {
      id:     self.id,
      title:  self.title,
      body:   self.body,
      next_id: next_news.nil? ? 0 : next_news.id,
      prev_id: prev_news.nil? ? 0 : prev_news.id
    }
  end
end

